package jm.nj.demo14mian.demo04;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

/*
* TCP文件服务器
* */
public class DemoFileServerSocket {
    public static void main(String[] args) throws IOException {
        //1.创建服务器,等待客户端连接
        Random kl=new Random(9999);

        ServerSocket serverSocket = new ServerSocket(8888);
        //用船接
        Socket clientSocket = serverSocket.accept();

        //显示那个客户端连接了
        InetAddress ipObject = clientSocket.getInetAddress();
        String ip = ipObject.getHostAddress();

        System.out.println("当前客户端:"+ip);

        //获取输出流
        InputStream in = clientSocket.getInputStream();

        //创建输出流地址(目的地)
        BufferedOutputStream fileout = new BufferedOutputStream(new FileOutputStream("F:\\"+kl.nextInt()+System.currentTimeMillis()+".jpg"));
        //把Socket 输入流中的数据流,写入目的地
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = in.read(buffer))!=-1){
            fileout.write(buffer,0,len);
        }
       // clientSocket.shutdownOutput();
        //-----------------以下为反馈-------------

        OutputStream out = clientSocket.getOutputStream();
        out.write("图片上传成功".getBytes());

        //out.close();
        //fileout.close();
        //in.close();
        //clientSocket.close();







    }
}
